﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PIT.Labs.SharePoint.Extensions
{
    
    /// <summary>
    /// This class contains "<a href="http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.90%29.aspx">Extension Methods</a>" for <see cref="Microsoft.SharePoint.SPGroup"/>.
    /// </summary>
    public static class SPGroupExtension
    {
        /// <summary>
        /// Checks, if a particular user is member of the group.
        /// </summary>
        /// <param name="spGroup">Represents a SharePoint Foundation group.</param>
        /// <param name="user">Represents a user in Microsoft SharePoint Foundation.</param>
        /// <returns><see cref="Boolean"/><br />
        /// <b>true</b>, if the user is found in the group, otherwise <b>false</b>.
        /// </returns>
        [CLSCompliantAttribute(false)]
        public static bool ContainsUser(this SPGroup spGroup, SPUser user)
        {
            bool isExisting = false;
            if (spGroup != null && user != null)
            {
                try
                {
                    SPUser tmpUser = spGroup.Users.GetByID(user.ID);
                    isExisting = true;
                }
                catch (SPException ex)
                {
                    if (ex.ErrorCode != -2146232832)
                        throw;
                }
            }
            return isExisting;
        }
    }
}
